home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / DTS QT Utilities.Aug-95 / Projects & Test Apps / CompressMovies / Tool Framework / ToolFramework.c next >
Encoding:
C/C++ Source or Header  |  1995-04-02  |  6.4 KB  |  252 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        ToolFramework.c
  3.  
  4.     Contains:    Simple AE framework for QuickTime related tools.
  5.  
  6.     Written by:    DTS
  7.  
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.        <1>         1/30/95    khs        first file
  13.        
  14. */
  15.  
  16.  
  17. // INCLUDES
  18. #include "ToolFramework.h"
  19. #include "DTSQTUtilities.h"
  20. #include "CompressMovie.h"
  21.  
  22. // GLOBALS AND CONSTANTS
  23. Boolean gOneShot = true;            // Will we trigger this application just once, or is it OK to keep the app open (need 
  24.                                                 // a later quit AE message then.
  25.  
  26. Boolean gDone = false;
  27. unsigned long gWNEsleep = 0;        
  28. Boolean gHasAppleEvents = false;
  29.  
  30.  
  31. // ______________________________________________________________________
  32. // MAIN
  33. void main(void)
  34. {    
  35.     OSErr anErr;
  36.         
  37.     InitMacEnvironment(10L);        
  38.  
  39.     if (!InitializeAppleEvents())
  40.         ExitToShell();
  41.         
  42.     if( !QTUIsQuickTimeInstalled() )
  43.         ExitToShell();
  44.  
  45. #if powerc    
  46.     if( !QTUIsQuickTimeCFMInstalled() )
  47.         ExitToShell();                                
  48. #endif 
  49.  
  50.     anErr = EnterMovies(); DebugAssert(anErr == noErr);
  51.     if(anErr != noErr)
  52.         ExitToShell();
  53.  
  54.     MainEventLoop();
  55. }
  56.  
  57.  
  58. // ______________________________________________________________________
  59. pascal void InitMacEnvironment(long nMasters)
  60. {
  61.     long    i;
  62.     MaxApplZone();
  63.     
  64.     for(i = 0; i <nMasters; i++)
  65.         MoreMasters();
  66.     
  67.     InitGraf(&qd.thePort);
  68.     InitFonts();
  69.     InitWindows();
  70.     InitMenus();
  71.     FlushEvents(everyEvent, 0);
  72.     TEInit();
  73.     InitCursor();
  74.     InitDialogs(NULL);
  75. }
  76.  
  77.  
  78. // ______________________________________________________________________
  79. pascal Boolean InitializeAppleEvents(void)
  80. {
  81.     OSErr     anErr;
  82.     long        aVersion;
  83.     
  84.     anErr = Gestalt(gestaltAppleEventsAttr, &aVersion); DebugAssert(anErr == noErr);
  85.     if(anErr != noErr)
  86.         return false;        // Apple Event Manager is not present on the system.
  87.         
  88.     if( !(aVersion & (1L << gestaltAppleEventsPresent)))
  89.         return false;        // The current configuration does not support Apple Events.
  90.         
  91.     // Continue installing our core event handlers.
  92.     gHasAppleEvents = true;
  93.  
  94.     anErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, 
  95.                                                     NewAEEventHandlerProc(AEOpenHandler), 0, false);
  96.     DebugAssert(anErr == noErr);
  97.     if(anErr)
  98.         return false;
  99.  
  100.     anErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, 
  101.                                                     NewAEEventHandlerProc(AEOpenDocHandler), 0, false);
  102.     DebugAssert(anErr == noErr);
  103.     if(anErr)
  104.         return false;
  105.  
  106.     anErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  107.                                                      NewAEEventHandlerProc(AEQuitHandler), 0, false);
  108.     DebugAssert(anErr == noErr);
  109.     if(anErr)
  110.         return false;
  111.  
  112.     anErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, 
  113.                                                     NewAEEventHandlerProc(AEPrintHandler), 0, false);
  114.     DebugAssert(anErr == noErr);
  115.     if(anErr)
  116.         return false;
  117.             
  118.     return true;
  119. }
  120.  
  121.  
  122. // ______________________________________________________________________
  123. pascal void MainEventLoop(void)
  124. {
  125.     EventRecord anEvent;
  126.     
  127.     while(!gDone)
  128.     {
  129.         SystemTask();
  130.         WaitNextEvent(everyEvent, &anEvent, gWNEsleep, NULL);
  131.         
  132.         switch(anEvent.what)
  133.         {
  134.         // We are only interested in high level events.
  135.             case kHighLevelEvent:
  136.                 if(gHasAppleEvents)
  137.                     AEProcessAppleEvent(&anEvent);
  138.                     break;
  139.             
  140.             default:
  141.                 DebugAssert("we should not get any events here");
  142.                 break;
  143.         }
  144.     }
  145. }
  146.  
  147.  
  148. // ______________________________________________________________________
  149. // THE AE HANDLERS
  150.  
  151. // ______________________________________________________________________
  152. pascal OSErr AEOpenHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  153. {
  154. // We are calling a stub function that supposedly will handle the open case (usually creating a new entity)
  155. // Default we do nothing.
  156.     return errAEEventNotHandled;
  157. }
  158.  
  159.  
  160. // ______________________________________________________________________
  161. pascal OSErr AEOpenDocHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  162. {
  163. // Parse the incoming entries (could be more than one, and call a specific function for each incoming entry.
  164.     OSErr             anErr;
  165.     AEDescList    aDocumentList;
  166.     AEKeyword    aKeyword;
  167.     DescType        aTypeCode;
  168.     Size                actualSize;
  169.     long                nDocuments, index;
  170.     FSSpec            anFSSpec;
  171.     
  172.     anErr = AEGetParamDesc(theMessage, keyDirectObject, typeAEList, &aDocumentList); DebugAssert(anErr == noErr);
  173.     if(anErr != noErr) return anErr;
  174.     
  175.     anErr = CheckForRequiredAEParams(theMessage); DebugAssert(anErr == noErr);
  176.     if(anErr != noErr)
  177.     {
  178.         anErr = AEDisposeDesc(&aDocumentList); DebugAssert(anErr == noErr);
  179.          return anErr;
  180.     }
  181.     anErr = AECountItems(&aDocumentList, &nDocuments); DebugAssert(anErr == noErr);
  182.     if(anErr != noErr) 
  183.     {
  184.         anErr = AEDisposeDesc(&aDocumentList); DebugAssert(anErr == noErr);
  185.         return anErr;
  186.     }
  187.     
  188.     for(index = 1; index <= nDocuments; index++)
  189.     {
  190.         anErr = AEGetNthPtr(&aDocumentList, index, typeFSS, &aKeyword, &aTypeCode,(Ptr)&anFSSpec,
  191.                                             sizeof(FSSpec), &actualSize); DebugAssert(anErr == noErr);
  192.         if(anErr != noErr)
  193.             return anErr;
  194.         
  195.     // Call a function and pass the obtained FSSpec.
  196.         if(index == 1)
  197.             SetFirstRecompressState(true);        // first time we run through the movies (or movie)
  198.         else
  199.             SetFirstRecompressState(false);        // if this is the second, or N:th time, set this to false
  200.             
  201.         anErr = RecompressMovieFile(&anFSSpec); DebugAssert(anErr == noErr);
  202.         if(anErr != noErr)
  203.             return anErr;
  204.     }
  205.     
  206.     if(gOneShot)
  207.         gDone = true;
  208.     
  209.     anErr = AEDisposeDesc(&aDocumentList); DebugAssert(anErr == noErr);
  210.     
  211.     return noErr;
  212. }
  213.  
  214.  
  215. // ______________________________________________________________________
  216. pascal OSErr AEPrintHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  217. {
  218. // We are calling a stub function that supposedly will handle the print case (usually printing a known entity)
  219. // Default we do nothing.
  220.     return errAEEventNotHandled;
  221. }
  222.  
  223.  
  224. // ______________________________________________________________________
  225. pascal OSErr AEQuitHandler(AppleEvent *theMessage, AppleEvent *theReply, long refCon)
  226. {
  227. // If we need to do any cleanup when quit:ing, do it here.
  228.     gDone = true;
  229.     
  230.     return noErr;
  231. }
  232.  
  233.  
  234. // ______________________________________________________________________
  235. // ADDITIONAL AE FUNCTIONS
  236. // ______________________________________________________________________
  237. pascal OSErr CheckForRequiredAEParams(AppleEvent *theEvent)
  238. {
  239.     DescType    returnedType;
  240.     Size            actualSize;
  241.     OSErr        anErr;
  242.     
  243.     anErr = AEGetAttributePtr(theEvent, keyMissedKeywordAttr, typeWildCard, &returnedType, 
  244.                                                 NULL, 0, &actualSize); 
  245.     if(anErr == errAEDescNotFound)    // all the parameters were there!
  246.         return noErr;
  247.     else
  248.         if(anErr == noErr)                        // missed parameters
  249.             return errAEParamMissed;
  250.         else
  251.             return anErr;                        // the call to AEGetAttributePtr failed
  252. }